﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VentaElectrodomesticos.Sql;
using System.Data.SqlClient;

namespace VentaElectrodomesticos.AbmRol
{
    public partial class FormSeleccionRol : Form
    {
        ClaseSQL sql = new ClaseSQL();
        private String tipoForm;

        public FormSeleccionRol(String tipoForm, bool mostrarDeshabilitados)
        {
            InitializeComponent();
            this.tipoForm = tipoForm;

            if (tipoForm.Equals("baja"))
            {
                this.Text = "Formulario de baja de roles";
                this.seleccionar.HeaderText = "Eliminar";
                this.seleccionar.Text = "Eliminar";
                this.seleccionar.UseColumnTextForButtonValue = true;
            }

            try
            {
                sql.Open();
                string query = "SELECT idRol, nomRol, status FROM LAG.roles";
                if (!mostrarDeshabilitados)
                    query += " WHERE status = '1'";

                SqlDataReader reader = sql.busquedaSQLDataReader(query);

                while (reader.Read())
                {
                    dataGridView1.Rows.Add(
                        reader["idRol"],
                        reader["nomRol"].ToString(),
                        reader["status"]
                    );
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error al buscar roles");
            }
            finally
            {
                sql.Close();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignorar clicks que no son en la columna seleccionar 
            if (e.RowIndex < 0 || e.ColumnIndex != dataGridView1.Columns["seleccionar"].Index) return;

            Rol rol = new Rol(dataGridView1.Rows[e.RowIndex]);

            if (tipoForm.Equals("modificacion"))
            {
                FormAltaRol formModificacionRol = new FormAltaRol(rol, true);
                formModificacionRol.ShowDialog(this);
            }
            else if (tipoForm.Equals("baja"))
            {
                try
                {
                    sql.Open();
                    String[,] parametros = new String[2, 1];
                    String sp = "LAG.sp_bajaRol";
                    parametros[0, 0] = "@ID";
                    parametros[1, 0] = dataGridView1[0, e.RowIndex].Value.ToString();
                    SqlDataReader reader = sql.ejecutarStoredProcedure(sp, parametros);
                    if (reader != null)
                    {
                        MessageBox.Show("Se ha dado de baja el Rol " + dataGridView1[1, e.RowIndex].Value.ToString(), "Success");
                        this.dataGridView1.Rows.RemoveAt(this.dataGridView1.CurrentRow.Index);
                    }

                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error app");
                }
                finally
                {
                    sql.Close();
                }
            }
            
        }


    }
}
